/**
 * Notes: 初始化控制器 - 用于初始化门禁设备等数据
 * Date: 2025-01-27
 */

const BaseController = require('./base_controller.js');
const DeviceModel = require('../model/device_model.js');
const StoreModel = require('../model/store_model.js');
const timeUtil = require('../../framework/utils/time_util.js');

class InitController extends BaseController {

	/**
	 * 初始化门禁设备
	 */
	async initAccessDevices() {
		try {
			console.log('开始初始化门禁设备数据...');
			
			// 门禁设备配置
			const accessDevices = [
				{
					_id: 'device_access_001',
					_pid: 'access_control_001',
					name: '门店1门禁',
					code: 'ACCESS_001',
					type: 'access_control',
					category: 'security',
					storeId: 'store_001',
					location: '门店入口',
					status: 1, // 可用状态
					isOnline: true,
					switchStatus: false,
					deviceMAC: '8cce4e518bf9',
					iotId: 'access_iot_001',
					appId: 'CPHgXm',
					deviceKey: 'kHKxitSJhWTz',
					clientId: '8cce4e518bf9',
					host: 'mqtt.geek-smart.cn',
					port: 1883,
					username: 'kHKxitSJhWTz',
					password: '',
					publishTopic: '/CPHgXm/kHKxitSJhWTz/8cce4e518bf9/publish',
					subscribeTopics: '/CPHgXm/kHKxitSJhWTz/8cce4e518bf9/subscribe',
				createTime: timeUtil.time(),
					updateTime: timeUtil.time()
				},
				{
					_id: 'device_access_002',
					_pid: 'access_control_002',
					name: '门店2门禁',
					code: 'ACCESS_002',
					type: 'access_control',
					category: 'security',
					storeId: 'store_002',
					location: '门店入口',
					status: 1, // 可用状态
					isOnline: true,
					switchStatus: false,
					deviceMAC: '8cce4e518bf8',
					iotId: 'access_iot_002',
					appId: 'CPHgXm',
					deviceKey: 'kHKxitSJhWTz2',
					clientId: '8cce4e518bf8',
					host: 'mqtt.geek-smart.cn',
					port: 1883,
					username: 'kHKxitSJhWTz2',
					password: '',
					publishTopic: '/CPHgXm/kHKxitSJhWTz2/8cce4e518bf8/publish',
					subscribeTopics: '/CPHgXm/kHKxitSJhWTz2/8cce4e518bf8/subscribe',
				createTime: timeUtil.time(),
					updateTime: timeUtil.time()
				}
			];
			
			let createdCount = 0;
			let skippedCount = 0;
			
			for (const deviceData of accessDevices) {
				try {
					// 检查设备是否已存在
					const existing = await DeviceModel.getAll({
						name: deviceData.name,
						type: 'access_control'
					}, '_id', {}, 1, false);
					
					if (existing && existing.length > 0) {
						console.log(`门禁设备 ${deviceData.name} 已存在，跳过创建`);
						skippedCount++;
						continue;
					}
					
					// 创建设备记录
					const deviceId = await DeviceModel.insert(deviceData);
					console.log(`门禁设备 ${deviceData.name} 创建成功，ID: ${deviceId}`);
					createdCount++;
					
				} catch (error) {
					console.error(`创建门禁设备 ${deviceData.name} 失败:`, error);
					throw error;
				}
			}
			
			console.log('门禁设备数据初始化完成！');
			return {
				success: true,
				message: '门禁设备数据初始化完成',
				createdCount,
				skippedCount
			};
			
		} catch (error) {
			console.error('初始化门禁设备数据失败:', error);
			throw error;
		}
	}

	/**
	 * 初始化门店数据
	 */
	async initStores() {
		try {
			console.log('开始初始化门店数据...');
			
			// 门店配置
			const stores = [
				{
					_id: 'store_001',
					_pid: 'store_pid_001',
					name: '超级泡泡蓝光店',
					address: '福建省福州市晋安区鼓山镇前横北路蓝光星珀9#19',
					location: {
						longitude: 119.3649,
						latitude: 26.0745
					},
					phone: '18558880680',
					openingHours: '09:00-22:00',
					coverImage: '',
					doorLockId: 'access_control_001',
					status: 1,
					createTime: timeUtil.time(),
					updateTime: timeUtil.time()
				},
				{
					_id: 'store_002',
					_pid: 'store_pid_002',
					name: '超级泡泡光年店',
					address: '福州市台江区望龙一路与长汀街交叉口左海·光年PARK商场B1-10铺',
					location: {
						longitude: 119.2649,
						latitude: 26.0645
					},
					phone: '18558880680',
					openingHours: '09:00-22:00',
					coverImage: '',
					doorLockId: 'access_control_002',
					status: 1,
					createTime: timeUtil.time(),
					updateTime: timeUtil.time()
				}
			];
			
			let createdCount = 0;
			let skippedCount = 0;
			
			for (const storeData of stores) {
				try {
					// 检查门店是否已存在
					const existing = await StoreModel.getAll({
						_id: storeData._id
					}, '_id', {}, 1, false);
					
					if (existing && existing.length > 0) {
						console.log(`门店 ${storeData.name} 已存在，跳过创建`);
						skippedCount++;
						continue;
					}
					
					// 创建门店记录
					const storeId = await StoreModel.insert(storeData);
					console.log(`门店 ${storeData.name} 创建成功，ID: ${storeId}`);
					createdCount++;
					
				} catch (error) {
					console.error(`创建门店 ${storeData.name} 失败:`, error);
					throw error;
				}
			}
			
			console.log('门店数据初始化完成！');
			return {
				success: true,
				message: '门店数据初始化完成',
				createdCount,
				skippedCount
			};
			
		} catch (error) {
			console.error('初始化门店数据失败:', error);
			throw error;
		}
	}
}

module.exports = InitController;